package com.eonmind.knowledge.mapper;

import com.eonmind.common.contract.data.base.BaseMongoMapper;
import com.eonmind.common.contract.enums.DelEnum;
import com.eonmind.common.provider.entity.knowledge.KnowledgeUploadFile;
import com.mongoplus.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Component;

/**
 * @Author: AKang
 * @Description:
 * @CreateTime: 2025-05-28
 */
@Component
public class KnowledgeUploadFileMapper extends BaseMongoMapper<KnowledgeUploadFile> {
    /**
     * 保存知识文件上传信息
     * @param knowledgeUploadFile
     */
    public void saveKnowledgeUploadFile(KnowledgeUploadFile knowledgeUploadFile){
        save(knowledgeUploadFile);
    }

    /**
     * 删除知识库时，级联删除该知识库所关联的文件上传信息
     * @param knowledgeId
     */
    public void deleteKnowledgeUploadFile(Long knowledgeId){
        UpdateWrapper<KnowledgeUploadFile> deleteWrapper = createMgoUpdate()
                .eq(KnowledgeUploadFile::getKnowledgeId, knowledgeId)
                .set(KnowledgeUploadFile::getDel, DelEnum.DEL.getCode());
        update(deleteWrapper);
    }

    /**
     * 获取指定知识库所关联的文件数量
     * @param knowledgeId
     * @return
     */
    public Long getKnowDocumentCount(Long knowledgeId){
        return count(createMgoQuery()
                .eq(KnowledgeUploadFile::getKnowledgeId, knowledgeId)
                .eq(KnowledgeUploadFile::getDel, DelEnum.UN_DEL.getCode()));
    }

    /**
     * 上传后更新知识文件上传过后获取的信息
     * @param knowledgeUploadFile
     */
    public void updateKnowledgeUploadFile(KnowledgeUploadFile knowledgeUploadFile){
        UpdateWrapper<KnowledgeUploadFile> updateWrapper = createMgoUpdate()
                .eq(KnowledgeUploadFile::getId, knowledgeUploadFile.getId())
                .set(KnowledgeUploadFile::getRealName, knowledgeUploadFile.getRealName())
                .set(KnowledgeUploadFile::getFileStatus, knowledgeUploadFile.getFileStatus())
                .set(KnowledgeUploadFile::getFileSize, knowledgeUploadFile.getFileSize());
        update(updateWrapper);
    }
}
